<template>
  <div class="page-container">
    <el-row :gutter="15">
      <el-col :span="6" :xs="24">
        <div class="box-con user">
          <div class="icon">
            <svg-icon name="user"></svg-icon>
          </div>
          <div class="rcn">
            <div class="num">{{ sysInfo?.userCount }}</div>
            <div class="title">{{ $t("module.user") }}</div>
          </div>
        </div>
      </el-col>
      <el-col :span="6" :xs="24">
        <div class="box-con cluster">
          <div class="icon">
            <svg-icon name="cluster"></svg-icon>
          </div>
          <div class="rcn">
            <div class="num">{{ sysInfo?.deptCount}}</div>
            <div class="title">{{ $t("module.dept") }}</div>
          </div>
        </div>
      </el-col>
      <el-col :span="6" :xs="24">
        <div class="box-con team">
          <div class="icon">
            <svg-icon name="team"></svg-icon>
          </div>
          <div class="rcn">
            <div class="num">{{ sysInfo?.roleCount }}</div>
            <div class="title">{{ $t("module.role") }}</div>
          </div>
        </div>
      </el-col>
      <el-col :span="6" :xs="24">
        <div class="box-con addteam">
          <div class="icon">
            <svg-icon name="addteam"></svg-icon>
          </div>
          <div class="rcn">
            <div class="num">{{ sysInfo?.postCount }}</div>
            <div class="title">{{ $t("module.post") }}</div>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="15" style="margin-top: 20px">
      <el-col :span="24" :xs="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>{{$t('user.register')}}</span>
            </div>
          </template>
          <client-only>
            <div
              class="chart"
              ref="userChart"
              style="width: 100%; height: 320px;"
            ></div>
          </client-only>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="15" class="ftc" style="margin-top: 20px">
      <el-col :span="6" :xs="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>联系信息</span>
            </div>
          </template>
          <div class="line-height">
            <p>
              <i class="el-icon-s-promotion"></i> 官网：<el-link
                href="https://www.ifrontend.net/"
                target="_blank"
                >https://www.ifrontend.net/</el-link
              >
            </p>
            <p>
              <i class="el-icon-s-promotion"></i> 仓库：<el-link
                href="https://gitee.com/tangweiwei/nuxt3-admin-template.git"
                target="_blank"
                >nuxt3-admin-template</el-link
              >
            </p>
            <p><i class="el-icon-user-solid"></i> QQ： 844475003</p>
            <p><i class="el-icon-chat-dot-round"></i> 微信：18681496678</p>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>更新日志</span>
            </div>
          </template>
          <el-timeline style="max-width: 600px">
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :color="activity.color"
              :timestamp="activity.timestamp"
            >
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
      <el-col :span="10" :xs="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>捐赠支持</span>
            </div>
          </template>
          <img src="@/assets/pay.png" style="width: 100%" />
          <p style="margin-top: 10px; text-align: center">
            您可以请作者喝杯咖啡表示鼓励
          </p>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref } from "#imports";

definePageMeta({
  title: "仪表盘",
  en_title: "Dashboard",
  affix: true,
});

const activities = [
  {
    content: "nuxt3-admin-template 中后台模板库，V1.0.0开源",
    timestamp: "2024-11-26",
    color: "#0bbd87",
  },
  {
    content:
      "Api服务开发进行中...... 技术栈：采用nuxt3自带的服务，数据库模块：@prisma/client、mysql。",
    timestamp: "2025-01-01",
  },
];

const { $eChart } = useNuxtApp();
const userChart = ref<HTMLElement | null>(null);
const renderChart = (categoryData: Array<string>, seriesData: Array<number>) => {
  const chart = $eChart.init(userChart.value as HTMLElement);
  const option = {
    grid: {
      top: 40,
      left: 20,
      right: 20,
      bottom: 20,
      containLabel: true,
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: categoryData,
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        data: seriesData,
        type: 'line',
        symbolSize: 10,
        symbol: 'circle',
        smooth: true,
        color: '#409eff',
        lineStyle: {
          width: 3,
          shadowColor: 'rgba(0,0,0,0.3)',
          shadowBlur: 10,
          shadowOffsetY: 8
        }
      }
    ]
  };

  chart.setOption(option);
}

const sysInfo = ref({
  userCount: 0,
  roleCount: 0,
  postCount: 0,
  deptCount: 0,
})
const { data: sysInfoData } = useAsyncData('sysInfo', async () => await statisticsSysCounts());
const result: any = sysInfoData.value;
if (result?.code === 200) {
  sysInfo.value = result.data;
}

onMounted(async () => {
  loadChartData();
})

const loadChartData = async () => {
  const result: any = await statisticsUserReg();
  const categoryData: Array<string> = [];
  const seriesData: Array<number> = [];
  if (result?.code === 200) {
    result.data.forEach((item: any) => {
      categoryData.push(item.date);
      seriesData.push(item.count);
    })
    renderChart(categoryData, seriesData);
  }
}

</script>

<style lang="less" scoped>
.box-con {
  background-color: var(--el-bg-color);
  border-radius: 4px;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  height: 80px;
  &.user {
    .icon {
      background-color: #409eff;
    }
    .num {
      color: #409eff;
    }
  }
  &.cluster {
    .icon {
      background-color: #67c23a;
    }
    .num {
      color: #67c23a;
    }
  }
  &.team {
    .icon {
      background-color: #e6a23c;
    }
    .num {
      color: #e6a23c;
    }
  }
  &.addteam {
    .icon {
      background-color: #f56c6c;
    }
    .num {
      color: #f56c6c;
    }
  }
  .svg-icon {
    font-size: 40px;
    color: #fff;
  }
  .icon {
    width: 80px;
    height: 80px;
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .rcn {
    flex: 1;
    padding: 15px;
    .num {
      font-size: 28px;
      line-height: 32px;
      font-weight: bold;
    }
  }
}
.page-container {
  a.el-link {
    color: var(--el-color-primary);
  }
  .line-height {
    line-height: 24px;
  }
  :deep(.ftc .el-card__body) {
    min-height: 320px;
  }
}

</style>
